package com.grieous.wow.web.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grieous.wow.biz.ContentBiz;
import com.grieous.wow.common.Page;
import com.grieous.wow.entity.Content;
import com.grieous.wow.entity.PageModel;
import com.grieous.wow.entity.Tag;
import com.grieous.wow.entity.User;

@Controller
@RequestMapping("/content")
public class ContentController {
	@Resource
	private ContentBiz contentBiz;

	@ExceptionHandler
	public String exp(Exception ex, HttpServletRequest request) {
		request.setAttribute("ex", ex);
		return "error";
	}

	// 内容管理
	@RequestMapping(value = "/manage")
	public String manage(Integer page, Integer pageSize,
			HttpServletRequest request, String openTime, String status,
			Integer tag, String tagJson, Model model) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			List<Tag> tagList = contentBiz.getTagList();
			model.addAttribute("tagList", tagList);
			PageModel pageModel = contentBiz.getContent(Page.PAGESIZE_DEFAULT,
					page, openTime, status, null, tag);
			model.addAttribute("pageModel", pageModel);
			model.addAttribute("status", status);
			model.addAttribute("openTime", openTime);
			model.addAttribute("tag", tag);
			return "admin/content/content";
		}
		return null;
	}

	// 增加内容
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			return "admin/content/saveContent";
		}
		return null;
	}

	// 编辑内容
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Integer id, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			Content content = contentBiz.getContentById(id);
			if (content != null) {
				request.setAttribute("content", content);
			}
			return "admin/content/saveContent";
		}
		return null;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String saveContent(Content content, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (contentBiz.saveContent(content))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delContent(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (contentBiz.delContent(ids))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 审核
	@RequestMapping("/approve")
	public String approveContent(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (contentBiz.approveContent(ids))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 打标签
	@RequestMapping("/tag")
	public String tagContent(int[] ids, String tag, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (contentBiz.tagContent(tag, ids))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 设置广告截至日期
	@RequestMapping("/deadline")
	public String deadlineContent(int[] ids, String deadline,
			HttpServletRequest request, HttpServletResponse response) {

		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (contentBiz.deadlineContent(deadline, ids))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/post")
	public String post() {

		return "post/postContent";
	}

	@RequestMapping(value = "/post/save", method = RequestMethod.POST)
	public @ResponseBody
	String postSave(Content content) {
		if (contentBiz.saveContent(content))
			return new String("success");
		return "fail";
	}
}
